--测试环境
-- package.path = package.path .. ";?.lua"
--生产环境
package.path = "./plugin/ETT0x03/utils.lua" .. ";..\\?.lua"
package.path = "./plugin/ETT0x03/json.lua" .. ";..\\?.lua"


require("utils")
require("json")

local pt

function TestEnv()
    --true:测试环境  false:生产环境
    if (string.find(package.path, "utils") ~= nil)
    then
        return false
    else
        return true
    end
end

if (TestEnv() == true)
then
    rxBuf = { 0x01, 0x03, 0x0F, 0x01, 0x94, 0xB8, 0x7B } --01 03 02 01 94 B8 7B
else
    rxBuf = {}
end

function Luaprint(str)
    --只在测试环境下的print
    if (TestEnv() == true)
    then
        print(str)
    end
end

-- Luaprint(package.path)
regAddr = 0x0000

function GetVariable(sAddr, step)
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if (step == 0)  --读取电能数据
    then
        requestADU[1] = sAddr --通信地址
        requestADU[2] = 0x03 --功能码
        requestADU[3] = 0x80 --寄存器起始地址·
        requestADU[4] = 0x00
        requestADU[5] = 0x00 --寄存器长度
        requestADU[6] = 0x28
        local VariableMapTemp = {
            Variable = requestADU
        }

        crc = GetCRCModbus(VariableMapTemp)
        if (crc == nil) then
            return VariableMap
        end

        requestADU[7] = math.modf(crc % 256)
        requestADU[8] = math.modf(crc / 256)

        VariableMap = {
            Status = "1",
            Variable = requestADU
        }

        regAddr = 0x8000
    elseif (step == 1)  --读取实时数据
    then
        requestADU[1] = sAddr --通信地址
        requestADU[2] = 0x03 --功能码
        requestADU[3] = 0x8D --寄存器起始地址·
        requestADU[4] = 0x00
        requestADU[5] = 0x00 --寄存器长度
        requestADU[6] = 0x40
        local VariableMapTemp = {
            Variable = requestADU
        }

        crc = GetCRCModbus(VariableMapTemp)
        if (crc == nil) then
            return VariableMap
        end

        requestADU[7] = math.modf(crc % 256)
        requestADU[8] = math.modf(crc / 256)

        VariableMap = {
            Status = "0",
            Variable = requestADU
        }

        regAddr = 0x8D00
    end

    local tmp = ""
    for i = 1, #requestADU do
        tmp = tmp .. " " .. string.format("%02X", requestADU[i])
    end

    return VariableMap
end

function GenerateGetRealVariables(sAddr, step)

    local VariableMap = GetVariable(sAddr,step)

    return VariableMap
end

function SetVariables(sAddr, cmdParam, step)
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    res = json.jsondecode(cmdParam)

    if (res["DO"] ~= nil) then
        if(step == 0) then
            value = tonumber(res["DO"])

            requestADU[1] = sAddr --通信地址
            requestADU[2] = 0x06 --功能码
            requestADU[3] = 0xA8 --寄存器起始地址·
            requestADU[4] = 0x22
            requestADU[5] = math.modf(value / 256)
            requestADU[6] = math.modf(value % 256)

            local VariableMapTemp = {
                Variable = requestADU
            }

            crc = GetCRCModbus(VariableMapTemp)
            if (crc == nil) then
                return VariableMap
            end

            requestADU[7] = math.modf(crc % 256)
            requestADU[8] = math.modf(crc / 256)

            VariableMap = {
                Status = "0",
                Variable = requestADU
            }

            regAddr = 0xA822
        end
    end

    return VariableMap
end

function DeviceCustomCmd(sAddr, cmdName, cmdParam, step)

    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(cmdName == "GetRealVariables") then
        VariableMap = GetVariable(sAddr,step)
    elseif(cmdName == "SetVariables") then
        VariableMap = SetVariables(sAddr, cmdParam, step)
    end

    local tmp = ""
    for i = 1, #requestADU do
        tmp = tmp .. " " .. string.format("%02X", requestADU[i])
    end

    return VariableMap
end

function AnalysisRx(sAddr, rxBufCnt)
    local mAddr = 0 --地址
    local mFunCode = 0 --功能码
    local mRegByte = 0 --有效字节数
    local variableMap = {
        Status = "1",
        Variable = {},
    }
    --字节数不足5位直接返回错误码
    if (rxBufCnt < 5) then
        rxBuf = {}
        return variableMap
    end
    for index = 1, rxBufCnt, 1 do
        mAddr = tonumber(rxBuf[index])
        mFunCode = rxBuf[index + 1]
        sAddr = tonumber(sAddr)
        if (mAddr == sAddr) then
            if (mFunCode == 0x03) then
                mRegByte = rxBuf[index + 2] --有效字节数
                if (rxBufCnt - 4 - index - mRegByte < 0) then
                    print("数据长度不够，没有数据域")
                    variableMap = {
                        Status = "1",
                        Variable = {}
                    }
                    rxBuf = {}
                    return variableMap
                end
                --取出数据域数据
                local pdu = {}
                for i = index, index + 2 + mRegByte do
                    if rxBuf[i] ~= nil then
                        table.insert(pdu, rxBuf[i])
                    end
                end

                local variable = {}
                local variableIndex = 1
                local dataIndex = index + 3
                if (regAddr == 0x8000) then
                    local regStartAddr = 0x8000
                    local temp = 0
                    ------------------------------------------------------------------------------------------
                    temp = rxBuf[dataIndex + 2*(0x8000-regStartAddr) ] * 16777216 +
                            rxBuf[dataIndex + 1 + 2*(0x8000-regStartAddr)] * 65536 +
                            rxBuf[dataIndex + 2 + 2*(0x8000-regStartAddr)] * 256 +
                            rxBuf[dataIndex + 3 + 2*(0x8000-regStartAddr)]
                    --local tmp = "EPP"
                    --tmp = tmp .. " " .. string.format("%02X", rxBuf[dataIndex + 2*(0x8000-regStartAddr) + (pIndex - 1) * 4])
                    --tmp = tmp .. " " .. string.format("%02X", rxBuf[dataIndex + 1 + 2*(0x8000-regStartAddr) + (pIndex - 1) * 4])
                    --tmp = tmp .. " " .. string.format("%02X", rxBuf[dataIndex + 2 + 2*(0x8000-regStartAddr) + (pIndex - 1) * 4])
                    --tmp = tmp .. " " .. string.format("%02X", rxBuf[dataIndex + 3 + 2*(0x8000-regStartAddr) + (pIndex - 1) * 4])
                    --print(tmp)
                    --取补码
                    local value1 = utils.Int32Complement(temp)
                    variable[variableIndex] = utils.AppendVariable(variableIndex, "EPP", value1, value1)
                    variableIndex = variableIndex + 1
                    ------------------------------------------------------------------------------------------
                    temp = rxBuf[dataIndex + 2*(0x801E-regStartAddr)] * 16777216 +
                            rxBuf[dataIndex + 1 + 2*(0x801E-regStartAddr)] * 65536 +
                            rxBuf[dataIndex + 2 + 2*(0x801E-regStartAddr)] * 256 +
                            rxBuf[dataIndex + 3 + 2*(0x801E-regStartAddr)]
                    --取补码
                    local value1 = utils.Int32Complement(temp)
                    variable[variableIndex] = utils.AppendVariable(variableIndex, "EQL", value1, value1)
                    variableIndex = variableIndex + 1

                    variableMap = {
                        Status = "0",
                        Variable = variable
                    }
                    rxBuf = {}
                    return variableMap
                elseif (regAddr == 0x8D00) then
                    local regStartAddr = 0x8D00
                    local temp = 0
                    ------------------------------------------------------------------------------------------
                    local params1 = { "Ua", "Ub", "Uc" }
                    local regAddrTab= {0x8D00,0x8D02,0x8D04}
                    for pIndex = 1, #params1, 1 do
                        temp = rxBuf[dataIndex + 0 + 2*(regAddrTab[pIndex]-regStartAddr)] * 16777216 +
                                rxBuf[dataIndex + 1 + 2*(regAddrTab[pIndex]-regStartAddr)] * 65536 +
                                rxBuf[dataIndex + 2 + 2*(regAddrTab[pIndex]-regStartAddr)] * 256 +
                                rxBuf[dataIndex + 3 + 2*(regAddrTab[pIndex]-regStartAddr)]
                        variable[variableIndex] = utils.AppendVariable(variableIndex, params1[pIndex], temp, temp)
                        variableIndex = variableIndex + 1
                    end
                    ------------------------------------------------------------------------------------------
                    params1 = { "Uab", "Ubc", "Uca" }
                    regAddrTab= {0x8D08,0x8D0A,0x8D0C}
                    for pIndex = 1, #params1, 1 do
                        temp = rxBuf[dataIndex + 0 + 2*(regAddrTab[pIndex]-regStartAddr)] * 16777216 +
                                rxBuf[dataIndex + 1 + 2*(regAddrTab[pIndex]-regStartAddr)] * 65536 +
                                rxBuf[dataIndex + 2 + 2*(regAddrTab[pIndex]-regStartAddr)] * 256 +
                                rxBuf[dataIndex + 3 + 2*(regAddrTab[pIndex]-regStartAddr)]
                        variable[variableIndex] = utils.AppendVariable(variableIndex, params1[pIndex], temp, temp)
                        variableIndex = variableIndex + 1
                    end

                    ------------------------------------------------------------------------------------------
                    params1 = { "Ia", "Ib", "Ic" }
                    regAddrTab= {0x8D0E,0x8D10,0x8D12}
                    for pIndex = 1, #params1, 1 do
                        temp = rxBuf[dataIndex + 0 + 2*(regAddrTab[pIndex]-regStartAddr)] * 16777216 +
                                rxBuf[dataIndex + 1 + 2*(regAddrTab[pIndex]-regStartAddr)] * 65536 +
                                rxBuf[dataIndex + 2 + 2*(regAddrTab[pIndex]-regStartAddr)] * 256 +
                                rxBuf[dataIndex + 3 + 2*(regAddrTab[pIndex]-regStartAddr)]
                        variable[variableIndex] = utils.AppendVariable(variableIndex, params1[pIndex], temp, temp)
                        variableIndex = variableIndex + 1
                    end

                    ------------------------------------------------------------------------------------------
                    params1 = { "Psum","Qsum","Ssum"}
                    regAddrTab= {0x8D1A,0x8D22,0x8D2A}
                    for pIndex = 1, #params1, 1 do
                        temp = rxBuf[dataIndex + 0 + 2*(regAddrTab[pIndex]-regStartAddr)] * 16777216 +
                                rxBuf[dataIndex + 1 + 2*(regAddrTab[pIndex]-regStartAddr)] * 65536 +
                                rxBuf[dataIndex + 2 + 2*(regAddrTab[pIndex]-regStartAddr)] * 256 +
                                rxBuf[dataIndex + 3 + 2*(regAddrTab[pIndex]-regStartAddr)]
                        --取补码
                        value1 = utils.Int32Complement(temp)
                        variable[variableIndex] = utils.AppendVariable(variableIndex, params1[pIndex], value1, value1)
                        variableIndex = variableIndex + 1
                    end

                    ------------------------------------------------------------------------------------------
                    params1 = { "PFsum"}
                    regAddrTab= {0x8D32}
                    for pIndex = 1, #params1, 1 do
                        temp = rxBuf[dataIndex + 0 + 2*(regAddrTab[pIndex]-regStartAddr)] * 256 +
                                rxBuf[dataIndex + 1 + 2*(regAddrTab[pIndex]-regStartAddr)]
                        --取补码
                        value1 = utils.Int16Complement(temp)
                        variable[variableIndex] = utils.AppendVariable(variableIndex, params1[pIndex], value1, value1)
                        variableIndex = variableIndex + 1
                    end

                    ------------------------------------------------------------------------------------------
                    params1 = { "FR"}
                    regAddrTab= {0x8D3F}
                    for pIndex = 1, #params1, 1 do
                        temp = rxBuf[dataIndex + 0 + 2*(regAddrTab[pIndex]-regStartAddr)] * 256 +
                                rxBuf[dataIndex + 1 + 2*(regAddrTab[pIndex]-regStartAddr)]
                        variable[variableIndex] = utils.AppendVariable(variableIndex, params1[pIndex], temp, temp)
                        variableIndex = variableIndex + 1
                    end

                    variableMap = {
                        Status = "0",
                        Variable = variable
                    }
                    rxBuf = {}
                    return variableMap
                end
            elseif(mFunCode == 0x06)
            then
                mRegAddr = rxBuf[index+2]*256+rxBuf[index+3]
                --取出数据域数据
                local pdu = {}
                for i=index,index+5,1 do
                    if rxBuf[i] ~= nil then
                        table.insert(pdu, rxBuf[i])
                    end
                end
                local VariableMapCRC = {
                    Variable = pdu
                }
                crc = GetCRCModbus(VariableMapCRC)
                crcTemp = rxBuf[index+6+1]*256+rxBuf[index+6]
                if(crc == crcTemp)
                then
                    VariableMap = {
                        Status = "0",
                        Variable = Variable
                    }
                    rxBuf = {}
                    return VariableMap
                end
            elseif (mFunCode == 0x85) then
                print("0x85发文发送异常！")
                variableMap = {
                    Status = "1",
                    Variable = {}
                }
                rxBuf = {}
                return variableMap
            end
        end
    end
    rxBuf = {}
    return variableMap
end

